<template>
  <div id="box">
    <div id="search">
      <el-form :inline="true" :model="scheduleSearch" class="demo-form-inline">
        <el-form-item label="开班时间">
          <el-date-picker
              style="width: 90%"
              v-model="scheduleSearch.searchParams.classTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开班时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSearchSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="header-bth">
      <el-button type="primary" size="small" icon=el-icon-circle-plus-outline @click="showAddDept">添加</el-button>
    </div>
    <div id="newClass-table">
      <el-table
          :data="tableData"
          stripe
          style="width: 100%">
        <el-table-column label="班级编号">
          <template slot-scope="scope">
            <span style="margin-left: 20px">{{ scope.row.classNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="授课老师">
          <template slot-scope="scope">
            <span>{{ scope.row.lecturer }}</span>
          </template>
        </el-table-column>
        <el-table-column label="班主任">
          <template slot-scope="scope">
            <span style="margin-left: 20px">{{ scope.row.principal }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开班时间" width="180px">
          <template slot-scope="scope">
            <span>{{ scope.row.classTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280px">
          <template slot-scope="scope">
            <el-button
                icon="el-icon-edit"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">
            </el-button>

            <el-popconfirm title="确定删除吗？" @confirm="deleteDept(scope.$index, scope.row)">
              <el-button
                  slot="reference"
                  size="mini"
                  type="danger"
                  icon="el-icon-delete">
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="nav-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :page-size="scheduleSearch.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--添加 编辑部门信息弹窗-->
    <el-dialog :title="dialogTitle" :visible.sync="addScheduleFormVisible" width="30%" @close="clearInfo">
      <el-form :model="scheduleForm" ref="scheduleInfo" :rules="scheduleRules" label-width="90px" size="medium">
        <el-input v-model="scheduleForm.id" autocomplete="off" v-show="false"></el-input>
        <el-form-item label="班级编号" prop="classNo">
          <el-input v-model="scheduleForm.classNo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="班主任" prop="principal">
          <el-input v-model="scheduleForm.principal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="教员" prop="lecturer">
          <el-input v-model="scheduleForm.lecturer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开班时间" prop="classTime">
          <el-date-picker
              style="width: 100%"
              v-model="scheduleForm.classTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开班时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>


import {addOrUpData, addOrUpDataClass, clazzPage, deleteClass} from "@/api/NewClassView";

export default {
  name: "NewClassView",
  data() {
    return {
      tableData: [],
      addScheduleFormVisible: false,
      dialogTitle: '',
      scheduleForm: {},
      scheduleSearch: {
        limit: 5,
        page: 1,
        searchParams:{
          classTime:''
        }
      },
      pageSizes: [ 5, 10,15],
      total: 10,
      scheduleRules: {
        classNo:[{required:true,message:'请输入班级编号',trigger:'blur'}],
        principal:[{required:true,message:'请输入班主任名称',trigger:'blur'}],
        lecturer:[{required:true,message:'请输入讲师名称',trigger:'blur'}],
        classTime:[{required:true,message:'请选择开班时间',trigger:'blur'}]
      }
    }
  },
  methods: {
    // 改变每页显示的条数触发
    handleSizeChange(val) {
      this.scheduleSearch.limit = val;
      this.list()
    },
    // 改变页码触发
    handleCurrentChange(val) {
      this.scheduleSearch.page = val;
      this.list()
    },
    //删除
    deleteDept(index, row) {
          deleteClass(row.id).then(response=>{
            if(response.code==0){
              this.$message.success(response.message);
              this.list()
            }
          })
    },
    // 显示弹窗
    showAddDept() {
      this.dialogTitle = '添加计划'
      this.addScheduleFormVisible = true
    },
    //编辑
    handleEdit(index, row) {
      this.dialogTitle = '编辑计划'
      this.addScheduleFormVisible = true
      //根据id 获取数据 回显数据
      this.scheduleForm=row

    },
    // 添加更新
    saveOrUpdate() {
      this.$refs.scheduleInfo.validate(valid=>{
        if (!valid) {
          this.$message.warning("请输入完整信息")
          return
        }
        // 发送异步请求  添加或修改开班计划
        addOrUpDataClass(this.scheduleForm).then(response=>{
          if (response.code==0){
            this.$message.success(response.message)
            this.addScheduleFormVisible = false
            this.list()
          }
        })

      })
    },
    // 搜索
    onSearchSubmit() {
      this.scheduleSearch.page = 1
      this.list()
    },
    // 关闭弹窗清空数据
    clearInfo() {
      this.scheduleForm = {}
      this.$refs.scheduleInfo.clearValidate()
    },
    list(){
       clazzPage(this.scheduleSearch).then(response=>{
         if (response.code==0){
           let {total,tableData}   =   response.data
           this.total=total
           this.tableData = tableData
         }
       })
    }
  },
  created() {
    this.list()
  }
}
</script>

<style scoped>

#box{
  width: 95%;
  margin: 5px auto 0;
}

#search {
  margin-top: 20px;
  padding: 5px;
  border-bottom: 1px solid #eee;
}

#header-bth {
  margin-top: 20px;
  margin-bottom: 20px;
}


#nav-page {
  margin-top: 15px;
  margin-bottom: 30px;
}

.el-button {
  margin-left: 5px;
  margin-right: 5px;
}
</style>

